<?php
require 'config.php';
require CAMINHO_SERVIDOR . 'funcoes.php';
require 'login.class.php';
$login = new Login();
if ($login->verificar(URL . 'index.php')) {
    if (!empty($_GET["id_time"])) {
        $query = "SELECT id_time,nome,img FROM tb_time WHERE id_time=" . $_GET["id_time"];
        $dadosTime = $login->executaQueryRow($query);
    }
    //funcoes::debuga($dadosTime);
}
$login->fecharConexao();
//funcoes::debuga($_GET);
?>
<html>
    <head>
        <title>Time</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/classes.css">
    </head>
    <body>
        <div class="container">
            <div class="container-fluid margin10px">
                <a class="btn btn-info offset0" href="<?php echo URL_ADMIN; ?>" >Início</a>
            </div>
            <form action="gravaTime.php" method="post" enctype='multipart/form-data'>
                <input id="id_time" type="hidden" value="<?php
                if (isset($_GET["id_time"])) {
                    echo $_GET["id_time"];
                }
                ?>" name="id_time"/>
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h3 class="panel-title text-center">Cadastrar Time</h3>
                        </div>
                        <div class="panel-body">
                            <div class="col-md-8 col-md-offset-2">
                                <div class="form-group">
                                    <label for="descricao">Nome do Time</label>
                                    <input class="form-control" required placeholder="Time" name="nome" id="nome" value="<?php echo (isset($dadosTime["nome"]) != "" ? $dadosTime["nome"] : ""); ?>" />
                                </div>
                                <div class="form-group">
                                    <label for="exampleInputFile">Imagem</label>
                                    <input required type="file" id="img" name="img">
                                    <?php
                                    if (isset($dadosTime['img'])) {
                                        ?>
                                        <img style='width: 100px;height: 100px; margin-top: 20px; margin-left:165px' src="<?php echo 'imagens/' . $dadosTime['img']; ?>"/>
                                        <?php
                                    }
                                    ?>
                                </div>
                                <?php
                                if (isset($_GET["msg"])) {
                                    if ($_GET["msg"] == 1) {
                                        ?>
                                        <div class="alert alert-success text-center">
                                            Time cadastrado com sucesso!
                                        </div>
                                        <?php
                                    } elseif ($_GET["msg"] == 2) {
                                        ?>
                                        <div class="alert alert-danger text-center">
                                            Algum erro ocorreu! Não foi salvo!
                                        </div>
                                        <?php
                                    } else if($_GET["msg"] == 3){
                                        ?>
                                        <div class="alert alert-success text-center">
                                            Time alterado com sucesso!
                                        </div>
                                        <?php
                                    } else {
                                        ?>
                                        <div class="alert alert-info text-center">
                                            Time já cadastrado!
                                        </div>
                                        <?php
                                    }
                                }
                                if (isset($_GET["id_time"])) {
                                    $texto_botao = "Alterar Time";
                                } else {
                                    $texto_botao = "Salvar Time";
                                }
                                ?>

                                <button class="btn btn-info col-md-5"  type="submit"><?php echo $texto_botao; ?></button>
                                <a class="btn btn-info col-md-5  col-md-offset-2" href="<?php echo URL; ?>listaTimes.php" >Listar Times</a>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>